<template>
  <div class="panel">
    <div class="panel-content">
      <div class="chart-container" ref="chartRef"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref<HTMLElement | null>(null)
let chart: echarts.ECharts | null = null

// 模拟数据
const cropData = {
  categories: ['生长速度', '产量预测', '叶面积', '茎粗', '株高'],
  current: [85, 92, 78, 88, 95],
  compare: [90, 85, 82, 85, 88]
}

const initChart = () => {
  if (!chartRef.value) return
  
  chart = echarts.init(chartRef.value)
  
  const option = {
    backgroundColor: 'transparent',
    tooltip: {
      trigger: 'item'
    },
    legend: {
      data: ['当前数据', '对比数据'],
      textStyle: {
        color: '#33ffd0'
      },
      right: '5%',
      bottom: '5%',
      orient: 'vertical',
      itemGap: 20
    },
    radar: {
      indicator: cropData.categories.map(name => ({ name, max: 100 })),
      splitArea: {
        areaStyle: {
          color: ['rgba(51, 255, 208, 0.1)']
        }
      },
      axisName: {
        color: '#33ffd0'
      },
      splitLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.2)'
        }
      },
      axisLine: {
        lineStyle: {
          color: 'rgba(51, 255, 208, 0.2)'
        }
      },
      center: ['50%', '55%'],
    },
    series: [{
      type: 'radar',
      data: [{
        value: cropData.current,
        name: '当前数据',
        areaStyle: {
          color: 'rgba(51, 255, 208, 0.3)'
        },
        lineStyle: {
          color: '#33ffd0',
          width: 2
        },
        itemStyle: {
          color: '#33ffd0'
        }
      },
      {
        value: cropData.compare,
        name: '对比数据',
        areaStyle: {
          color: 'rgba(255, 107, 107, 0.3)'
        },
        lineStyle: {
          color: '#ff6b6b',
          width: 2,
          type: 'dashed'
        },
        itemStyle: {
          color: '#ff6b6b'
        }
      }]
    }]
  }
  
  chart.setOption(option)
}

// 监听窗口大小变化
const handleResize = () => {
  chart?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
  
  // 模拟数据更新
  setInterval(() => {
    if (!chart) return
    
    const newCurrentValues = cropData.current.map(value => {
      const change = Math.random() * 10 - 5
      return Math.max(0, Math.min(100, value + change))
    })
    
    chart.setOption({
      series: [{
        data: [{
          value: newCurrentValues,
          name: '当前数据',
          areaStyle: {
            color: 'rgba(51, 255, 208, 0.3)'
          },
          lineStyle: {
            color: '#33ffd0',
            width: 2
          },
          itemStyle: {
            color: '#33ffd0'
          }
        },
        {
          value: cropData.compare,
          name: '对比数据',
          areaStyle: {
            color: 'rgba(255, 107, 107, 0.3)'
          },
          lineStyle: {
            color: '#ff6b6b',
            width: 2,
            type: 'dashed'
          },
          itemStyle: {
            color: '#ff6b6b'
          }
        }]
      }]
    })
  }, 3000)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  chart?.dispose()
})
</script>

<style scoped>
.panel {
  height: 100%;
  padding: 1rem;
  display: flex;
  flex-direction: column;
}

.panel-content {
  flex: 1;
  min-height: 0;
}

.chart-container {
  width: 100%;
  height: 100%;
}
</style> 